package com.study.jdbc.junit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;

import org.junit.Test;

import com.study.jdbc.utils.DBUtil;

/**
* @Name: LobTest
* @Description: Lob操作测试类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-17 下午7:31:25 
* @Version: V1.0
 */
public class LobTest {
	
	/**
	* @Name: saveLongText
	* @Description: 保存大数据
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-17 下午7:32:52
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void saveLob() {
		Connection con = null ;
		PreparedStatement ps = null ;
		try {
			//获取数据库连接对象
			con = DBUtil.getConnection() ;
			String sql = "insert into t_lob(id, bigText, image, vedio) values(?, ?, ?, ?) ; " ;
			//创建sql语句执行对象
			ps = con.prepareStatement(sql) ;
			//预处理
			ps.setString(1, UUID.randomUUID().toString()) ;
			//保存大文本数据
			File text = new File("lobFile/upload/log.txt") ;
			Reader reader = new FileReader(text) ;
			ps.setCharacterStream(2, reader, (int) text.length()) ;
			//保存图片
			File image = new File("lobFile/upload/HelloWorld.png") ;
			InputStream in = new FileInputStream(image) ;
			ps.setBinaryStream(3, in, (int) image.length()) ;
			//保存视频
			File vedio = new File("lobFile/upload/eclipse.avi") ;
			InputStream is = new FileInputStream(vedio) ;
			ps.setBinaryStream(4, is, (int) vedio.length()) ;
			//执行sql语句
			int result = ps.executeUpdate() ;
			if(result > 0) {
				System.out.println("保存成功！");
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		} finally {
			//释放资源
			DBUtil.close(con, ps, null) ;
		}
	}
	
	/**
	* @Name: readlob
	* @Description: 读取大数据
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-17 下午7:54:28
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void readlob() {
		Connection con = null ;
		PreparedStatement ps = null ;
		ResultSet rs = null ;
		InputStream is = null ;
		OutputStream os = null ;
		Reader reader = null ;
		Writer writer = null ;
		try {
			//获取数据库连接对象
			con = DBUtil.getConnection() ;
			String sql = "select bigText, image, vedio from t_lob where id = ? ; " ;
			//创建sql语句执行对象
			ps = con.prepareStatement(sql) ;
			//预处理
			String id = "8436e832-b0a8-4ef4-a5d3-27916451b26a" ;
			ps.setString(1, id) ;
			//执行sql语句，获取结果集对象
			rs = ps.executeQuery() ;
			//获取结果集
			if(rs.next()) {
				//获取大文本
				reader = rs.getCharacterStream("bigtext") ;
				writer = new FileWriter("lobFile/download/log.txt") ;
				char[] cbuf = new char[1024] ;
				int len = -1 ;
				while((len = reader.read(cbuf)) != -1) {
					writer.write(cbuf, 0, cbuf.length) ;
					writer.flush() ;
				}
				//获取图片
				is = rs.getBinaryStream("image") ;
				os = new FileOutputStream("lobFile/download/HelloWorld.png") ;
				byte[] buf = new byte[1024] ;
				while((len = is.read(buf)) != -1) {
					os.write(buf, 0, buf.length) ;
					os.flush() ;
				}
				//获取视频
				is = rs.getBinaryStream("vedio") ;
				os = new FileOutputStream("lobFile/download/eclipse.avi") ;
				while((len = is.read(buf)) != -1) {
					os.write(buf, 0, buf.length) ;
					os.flush() ;
				}
				System.out.println("获取成功！");
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		} finally {
			//释放资源
			DBUtil.close(con, ps, rs) ;
			try {
				reader.close() ;
				writer.close() ;
				is.close() ;
				os.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
	}
}
